#郑文超

import time,os
from PIL import ImageGrab
from common.zwc_login import Login
from testcase.financial_approval import financial_flow
from faker import Factory
from testcase.jiemi import quiry
from utils.utility import Utility
from common.report_generate import Report
from selenium.webdriver.support.select import Select
from selenium import webdriver
from common import setup

class Financial_account:
    def __init__(self,user):
        self.st = Login()
        self.driver = self.st.driver
        self.st.login_user(user)
        self.open_module("财务管理","账户管理")
        self.write_sql = Report()

    def open_module(self,big_module,small_module):
        self.driver.find_element_by_link_text(big_module).click()
        js = "var q=document.documentElement.scrollTop=10000"
        self.driver.execute_script(js)
        self.driver.implicitly_wait(5)
        time.sleep(2)
        self.driver.find_element_by_link_text(small_module).click()
        self.driver.implicitly_wait(3)
    def screenshot(self):
        pic_path = "../report/"
        filename = time.strftime('%Y%m%d_%H%M%S.png')
        ImageGrab.grab().save(pic_path + filename)
        return filename

    def random_select(self,foo,text):
        wd = Select(foo)  #实例化一个下拉框
        wd.select_by_visible_text(text)

    #修改账户状态为：停用
    def test_case1(self,s_num,title,caseid):
        self.driver.find_element_by_xpath("//*[@id='account-table']/tbody/tr[1]/td[8]/button").click()
        wd =self.driver.find_element_by_xpath("//*[@id='edit-form']/div/div[3]/div[1]/select")
        self.random_select(wd,s_num[0])
        list = self.driver.find_elements_by_xpath('//*[@id="addBtn"]')
        list[1].click()
        time.sleep(1)
        try:
            self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[3]/button")
            print("提示框")
            re = self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[2]/div").text
            if re =="修改成功":
                print("测试通过")
                self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")

            else:
                print("测试失败")
                img = self.screenshot()
                self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
            self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[3]/button").click()
        except:
            pass

        # 新增一个成都的专用私账的账户
        def test_case2(self, s_num, title, caseid):
            self.driver.find_element_by_xpath("//*[@id='accountInfo']/div[1]/div/div/button").click()
            wd = self.driver.find_element_by_xpath("//*[@id='add-form']/div/div[1]/div[1]/select")
            self.random_select(wd, s_num[0])
            wd1 = self.driver.find_element_by_xpath("//*[@id='add-form']/div/div[1]/div[2]/select")
            self.random_select(wd1, s_num[1])
            self.driver.find_element_by_xpath("//*[@id='add-form']/div/div[2]/div[1]/input")
            list = self.driver.find_elements_by_xpath('//*[@id="addBtn"]')
            list[1].click()
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[3]/button")
                print("提示框")
                re = self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[2]/div").text
                if re == "修改成功":
                    print("测试通过")
                    self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")

                else:
                    print("测试失败")
                    img = self.screenshot()
                    self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
                self.driver.find_element_by_xpath("/html/body/div[9]/div/div/div[3]/button").click()
            except:
                pass
# if __name__ == '__main__':
#     s = Financial_account()




